package com.zeen.wima.update;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.zeen.wima.R;

public class InstallList extends ListActivity {

	PluginAdapter adapter;
	ArrayList<DownloadTask> items = new ArrayList<DownloadTask>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.update_download_list);
	}

	@Override
	protected void onStart() {
		super.onStart();
		for (AppInfo pi : Updater.getNewUpdates()) {
			if (items.indexOf(pi) <= 0) {
				items.add(new DownloadTask(pi));
			}
		}
		adapter = new PluginAdapter(this, items);
		this.getListView().setAdapter(adapter);
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
	}

	public void onInstall(DownloadTask p) {
		items.remove(p);
		adapter.remove(p);
		adapter.notifyDataSetChanged();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (items.size() == 0) {
			finish();
		}
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			this.showDialog(BK_WARNING);
			return true;
		} else {
			return super.onKeyDown(keyCode, event);
		}
	}

	final int BK_WARNING = 0;

	@Override
	protected Dialog onCreateDialog(int id) {
		// TODO Auto-generated method stub
		AlertDialog.Builder dialog = null;
		switch (id) {
		case BK_WARNING:
			dialog = new AlertDialog.Builder(InstallList.this);
			dialog.setTitle(R.string.dialog_title_warning);
			dialog.setMessage(R.string.install_list_cancel_txt);
			dialog.setPositiveButton(R.string.dialog_button_no,
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,
								int whichButton) {
							dialog.dismiss();
						}
					});
			dialog.setNegativeButton(R.string.dialog_button_yes,
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,
								int whichButton) {
							dialog.dismiss();
							forceClose();
						}
					});
			break;
		}
		if (dialog != null)
			return dialog.create();
		else
			return null;
	}

	private void forceClose() {
		finish();
	}

	
	private class PluginAdapter extends BaseAdapter {
		private ArrayList<UpdateView> items;

		public void remove(DownloadTask p) {
			for (int i = 0; i < items.size(); i++) {
				UpdateView item = items.get(i);
				if (item.loadTask.equals(p)) {
					items.remove(item);
				}
			}
		}

		public PluginAdapter(Context context, ArrayList<DownloadTask> plugins) {
			super();
			items = new ArrayList<UpdateView>();
			for (DownloadTask p : plugins) {
				if (!p.hasDownloaded())
					items.add(new UpdateView(context, p));
			}
		}

		@Override
		public int getCount() {
			return items.size();
		}

		@Override
		public Object getItem(int position) {
			return items.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			return items.get(position);
		}
	}
}
